﻿package core.ui.controls
{
    import core.event.*;
    import core.image.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class ImageBox extends Sprite
    {
        public var item:ImageInfo;
        public var content:Sprite;
        var cwidth:int;
        var cheight:int;
        var scrollbar:Scrollbar;

        public function ImageBox(param1:int, param2:int)
        {
            this.cwidth = param1;
            this.cheight = param2;
            this.content = new Sprite();
            this.content.x = 1;
            this.content.y = 1;
            addChild(this.content);
            this.scrollbar = new Scrollbar(this.content, new Rectangle(0, 0, param1 - 2, param2 - 2));
            this.scrollbar.y = 1;
            this.scrollbar.x = param1 - 17;
            addChild(this.scrollbar);
            this.Resize(param1, param2);
            return;
        }// end function

        public function Resize(graphics:int, graphics:int) : void
        {
            this.cwidth = graphics;
            this.cheight = graphics;
            graphics.clear();
            graphics.beginFill(9474192);
            graphics.drawRect(0, 0, graphics, graphics);
            graphics.beginFill(16777215);
            graphics.drawRect(1, 1, graphics - 2, graphics - 2);
            graphics.beginBitmapFill(new InputBackground());
            graphics.drawRect(1, 1, graphics - 2, graphics > 22 ? (20) : (graphics - 2));
            graphics.endFill();
            this.scrollbar.SetSize(graphics - 2);
            this.scrollbar.x = graphics - 17;
            this.scrollbar.SetWidth(graphics - 2);
            return;
        }// end function

        public function AddItem(graphics:ImageDetails, graphics:String, graphics:String) : void
        {
            var _loc_4:* = new ImageInfo(graphics, graphics, graphics);
            _loc_4.doubleClickEnabled = true;
            _loc_4.mouseChildren = false;
            _loc_4.addEventListener(MouseEvent.CLICK, this.ItemClick, false, 1, true);
            _loc_4.addEventListener(MouseEvent.DOUBLE_CLICK, this.ItemDoubleClick, false, 1, true);
            var _loc_5:* = Math.floor(this.cwidth / (_loc_4.width + 10));
            var _loc_6:* = int(this.content.numChildren / _loc_5);
            var _loc_7:* = this.content.numChildren - _loc_6 * _loc_5;
            _loc_4.y = Math.round(_loc_6 * (_loc_4.height + 10)) + 10;
            _loc_4.x = Math.round(_loc_7 * (_loc_4.width + 10)) + 10;
            this.content.addChild(_loc_4);
            this.scrollbar.SetContentHeight(Math.ceil(this.content.numChildren / _loc_5) * (_loc_4.height + 10) + 10);
            return;
        }// end function

        public function RemoveItem() : void
        {
            if (this.item != null)
            {
                this.content.removeChild(this.item);
                this.item = null;
            }
            return;
        }// end function

        public function Rearrange() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:ImageInfo = null;
            var _loc_5:int = 0;
            if (this.content.numChildren > 0)
            {
                _loc_5 = 0;
                while (_loc_5 < this.content.numChildren)
                {
                    
                    _loc_4 = this.content.getChildAt(_loc_5) as ImageInfo;
                    _loc_1 = Math.floor(this.cwidth / (_loc_4.width + 10));
                    _loc_2 = int(_loc_5 / _loc_1);
                    _loc_3 = _loc_5 - _loc_2 * _loc_1;
                    _loc_4.y = Math.round(_loc_2 * (_loc_4.height + 10)) + 10;
                    _loc_4.x = Math.round(_loc_3 * (_loc_4.width + 10)) + 10;
                    _loc_5++;
                }
                this.scrollbar.SetContentHeight(Math.ceil(this.content.numChildren / _loc_1) * (_loc_4.height + 10) + 10);
            }
            return;
        }// end function

        public function Set(graphics:ImageInfo) : void
        {
            var _loc_2:int = 0;
            if (this.item != null)
            {
                this.item.Mark(false);
            }
            while (_loc_2 < this.content.numChildren)
            {
                
                this.item = this.content.getChildAt(_loc_2) as ImageInfo;
                if (this.item == graphics)
                {
                    break;
                }
                _loc_2++;
            }
            this.item.Mark();
            return;
        }// end function

        private function ItemClick(event:MouseEvent) : void
        {
            if (event.currentTarget != this.item)
            {
                if (this.item != null)
                {
                    this.item.Mark(false);
                }
                this.item = event.currentTarget as ImageInfo;
                this.item.Mark();
                dispatchEvent(new SelectEvent(SelectEvent.COMMAND, this.item.details.name));
            }
            return;
        }// end function

        private function ItemDoubleClick(event:MouseEvent) : void
        {
            if (this.item != null)
            {
                this.item.Mark(false);
            }
            this.item = event.currentTarget as ImageInfo;
            this.item.Mark();
            dispatchEvent(new SelectEvent(SelectEvent.EXECUTE, this.item.details.name));
            return;
        }// end function

        public function Clear() : void
        {
            while (this.content.numChildren > 0)
            {
                
                this.content.removeChildAt(0);
            }
            this.scrollbar.SetContentHeight(0);
            return;
        }// end function

        public function get value() : String
        {
            return this.item != null ? (this.item.details.id) : ("");
        }// end function

    }
}
